﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Utilities;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMQuestionAnswer.xaml
    /// </summary>
    public partial class VMQuestionAnswer : UserControl
    {
        public VMQuestionAnswer()
        {
            InitializeComponent();
            new PropertyChangeNotifier(this, FrameworkElement.DataContextProperty, (s, e) =>
            {
                GenerateQuestionAnswer();
            });
            this.DataContextChanged += (s, e) =>
                {
                    GenerateQuestionAnswer();
                };
            this.Loaded += new RoutedEventHandler(VMQuestionAnswer_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(VMQuestionAnswer_SizeChanged);
        }

        void VMQuestionAnswer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            var col1W = this.ActualWidth / 3;
            if (col1W > 100)
            {
                Grid_QuestionAnswer.ColumnDefinitions[0].Width = new GridLength(col1W);
            }
        }

        void VMQuestionAnswer_Loaded(object sender, RoutedEventArgs e)
        {
            //this.UpdateLayout();
            //Grid_QuestionAnswer.UpdateLayout();
            //UpdateBindingDataContext.UpdateDataContext(this);
        }
        
        public void GenerateQuestionAnswer()
        {
            var idata = (this.DataContext as IEnumerable<Step_QuestionAnwser>);
            var data =idata == null? null: idata.ToArray();
            if (data == null || data.Length == 0)
            {
                Grid_QuestionAnswer.Children.Clear();
            }
            else
            {
                GenerateRow(data);
            }            
        }
        private void GenerateRow(Step_QuestionAnwser[] data)
        {            
            if (data != null)
            {
                for (int i = 1; i <= data.Length; i++)
                {
                    bool needFocus = i == data.Length;
                    if (Grid_QuestionAnswer.RowDefinitions.Count < i)
                    {
                        var row = new RowDefinition() { };
                        if (data.Length < 4)
                            row.Height = new GridLength(1, GridUnitType.Star);
                        else
                            row.Height = new GridLength(1, GridUnitType.Auto);
                        Grid_QuestionAnswer.RowDefinitions.Add(row );
                    }
                    //data and binding
                    //textblock
                    VMTextBlockExt txt = new VMTextBlockExt() { Margin = new Thickness(2), TextWrapping = TextWrapping.Wrap };
                    //txt.SetBinding(VMTextBlock.TextProperty, new Binding(string.Format("[{0}].Question", i - 1)));
                    txt.TextPath=string.Format("[{0}].Question", i - 1);
                    var bfocus = needFocus && txt.Focus_Question();
                    Grid.SetColumn(txt, 0);
                    Grid.SetRow(txt, i-1);
                    Grid_QuestionAnswer.Children.Add(txt);   
                    //Cau lenh gan nay cuc ky nguy hiem
                   // txt.DataContext = data[i - 1];
                    //textbox
                    VMTextBox_Answer text = new VMTextBox_Answer() { Margin = new Thickness(2) };//, MinLines = 3};
                    //if (minHeight>0)
                    //text.MinHeight = minHeight;
                    //if (data.Length >= 4 && minHeight>10)
                    //    text.Height = minHeight - 4;
                    if (needFocus && bfocus == false)
                        text.Focus();
                    if (text is VMTextBox)
                    {
                        text.SetBinding(TextBox.TextProperty, new Binding(string.Format("[{0}].Answer", i - 1)));
                    }
                    if (text is VMRichTextBox)
                    {
                        text.SetBinding(VMRichTextBox.TextProperty, new Binding(string.Format("[{0}].Answer", i - 1)));
                    }
                    Grid.SetColumn(text, 1);
                    Grid.SetRow(text, i - 1);
                    Grid_QuestionAnswer.Children.Add(text);
                    //Cau lenh gan nay cuc ky nguy hiem
                    //text.DataContext = data[i - 1];
                    Image _image = new Image();
                    string packUri = "pack://application:,,,/Images/closex.png";
                    _image.Source = new ImageSourceConverter().ConvertFromString(packUri) as ImageSource;
                    _image.Width = 10;
                    Button Button_Remove = new Button()
                    {
                        Content =_image,
                        Margin = new Thickness(2),
                        VerticalAlignment = VerticalAlignment.Top,
                        OverridesDefaultStyle = true,
                        MinWidth = 0,
                        MinHeight = 0
                        ,
                        DataContext = data[i - 1],
                        Focusable = false, ToolTip = "Delete"
                    };
                    Grid.SetColumn(Button_Remove, 2);
                    Grid.SetRow(Button_Remove, i - 1);
                    Grid_QuestionAnswer.Children.Add(Button_Remove);
                    Button_Remove.Click += new RoutedEventHandler(Button_Remove_Click);
                    
                }
                //Refresh
                Grid_QuestionAnswer.UpdateLayout();
            }
        }
        public Action<Step_QuestionAnwser> Action_Remove_QA;
        public Action Action_AddNew_QA;
        void Button_Remove_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                #region focus vao.Hien tai neu dang dung o textbox, click xoa . click qua textbox khac thi du lieu lai dc phuc hoi. nen choi lui focus qua cho khac
                btn.Focusable = true;
                btn.Focus();
                btn.Focusable = false;
                #endregion
                Step_QuestionAnwser data = btn.DataContext as Step_QuestionAnwser;
                if (data != null)
                {
                    if (Action_Remove_QA != null)
                        Action_Remove_QA(data);
                }
            }
        }

        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            if (Action_AddNew_QA != null)
                Action_AddNew_QA();
        }
    }
}
